#include <gdk/gdkkeysyms.h>
#include <string.h>
#include "sms.h"

#define SETTING_PATH    "/usr/share/sms/default/layout/layout-setting/"
#define SETTING_LAYOUT_NAME "layout-setting"
#define LAN_NET_WORK_BG "Cable_Network-bg.png"
static int focus_id=0;

#define ANIMATE_STOP     1
#define ANIMATE_RESTART  2
#define ANIMATE_CONTINUE 3

typedef struct _Icon
{
  char *name;

  GooCanvasItem *icon;
  GooCanvasItem *icon_a;

  LAYOUT_WIN_ID id;

  char *cmd;
  char **argv;
  int argc;

  int x;
  int y;
  int size;
  int asize;
}Icon;

Icon icons[2];

typedef struct _Title{	
  int x1;
  int y1;
  int x2;
  int y2;
  int size;
  char *font;
  char *color;
  GooCanvasItem *text1;
  GooCanvasItem *text2;
}Title;

typedef struct _SetLayout_1
{
  GooCanvasItem *bg;
  int micon_count;
  int focus_id ;
  Icon icons[2];
  Exec *exec;
  Title title;

  Animate *animates;
  int animate_count;

  Layout *layout;
  char buf[512];
  //ListView *list;
  //GooCanvasItem *test;
}SetLayout;


static gboolean setting_layout_load_bg(xmlNode *a_node,Layout *layout)
{
  SetLayout *setlayout=layout->private;
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  if(setlayout->bg||!a_node)
    return FALSE;

  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
    return FALSE;

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,800,480);
  if(!pixbuf)
    return FALSE;

  setlayout->bg=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);    

  GooCanvasItem *wave ,*seaweed1 ,*seaweed2 ,*seaweed3 ,*ship ,*lan ,*cloud;

  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
      ,layout->name,"Clouds.png");
  GdkPixbuf  *pixbuf_cloud=gdk_pixbuf_new_from_file(pic ,NULL);
  cloud = goo_canvas_image_new(layout->cavas ,pixbuf_cloud ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(cloud ,84 ,40 ,1 ,0);

  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
      ,layout->name,"Network_Settings.png");
  GdkPixbuf *pixbuf_lan 
    = gdk_pixbuf_new_from_file(pic ,NULL);
  lan=goo_canvas_image_new(layout->cavas ,pixbuf_lan ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(lan ,90 ,40 ,1 ,0);

  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
      ,layout->name,"Ship.png");
  GdkPixbuf *pixbuf_ship = gdk_pixbuf_new_from_file(pic ,NULL);
  ship = goo_canvas_image_new(layout->cavas ,pixbuf_ship ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(ship ,360 ,0 ,1 ,0);
  //goo_canvas_item_animate(ship ,50 ,0 ,1 ,0 ,FALSE ,5000 ,40 ,
  //      GOO_CANVAS_ANIMATE_BOUNCE);


  /*sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default"
    ,layout->name,"Wave0.png");
    GdkPixbuf *pixbuf_wave = gdk_pixbuf_new_from_file(pic ,NULL);
    wave = goo_canvas_image_new(layout->cavas ,pixbuf_wave ,0 ,0 ,NULL);
  //goo_canvas_item_translate(wave ,0 ,103);
  goo_canvas_item_set_simple_transform(wave ,-800 ,90 ,1 ,0);
  goo_canvas_item_animate(wave ,800 ,0 ,1 ,0 ,FALSE ,40000 ,100 ,
  GOO_CANVAS_ANIMATE_BOUNCE);

  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
  ,layout->name,"Seaweed1.png");
  GdkPixbuf *pixbuf_sea1 =gdk_pixbuf_new_from_file(pic,NULL);
  seaweed1 = goo_canvas_image_new(layout->cavas ,pixbuf_sea1 ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(seaweed1 ,117 ,400 ,1 ,0);
  goo_canvas_item_animate(seaweed1 ,167 ,360 ,2 ,0 ,TRUE ,5000 ,80 ,
  GOO_CANVAS_ANIMATE_BOUNCE);

  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
  ,layout->name,"Seaweed.png");
  GdkPixbuf *pixbuf_sea2 =gdk_pixbuf_new_from_file(pic ,NULL);
  seaweed2 = goo_canvas_image_new(layout->cavas ,pixbuf_sea2 ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(seaweed2 ,464 ,451 ,1 ,0);
  goo_canvas_item_animate(seaweed2 ,400 ,390 ,2 ,0 ,TRUE ,5000 ,80 ,
  GOO_CANVAS_ANIMATE_BOUNCE);

  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" 
  ,layout->name,"Seaweed1.png");
  GdkPixbuf *pixbuf_sea3 =gdk_pixbuf_new_from_file(pic,NULL);
  seaweed3 = goo_canvas_image_new(layout->cavas ,pixbuf_sea3 ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(seaweed3 ,640 ,449 ,1 ,0);
  goo_canvas_item_animate(seaweed3 ,640 ,380 ,2 ,0 ,TRUE ,5000 ,80 ,
  GOO_CANVAS_ANIMATE_BOUNCE);*/

  return TRUE;

}


static void test_layout_create_pos(xmlNode *a_node,int count,int *pos[])
{
  int i,points;
  char key[16];
  xmlChar * val;

  points=sms_get_xml_int (a_node,(xmlChar*)"points",0);

  if(count<points)
    return ;

  *pos=(int *)g_malloc0(sizeof(int)*count+1);
  *(pos+1)=(int *)g_malloc0(sizeof(int)*count+1);

  for(i=0;i<count;i++)
  {
    sprintf(key,"p%d_x",i+1);
    pos[0][i] = sms_get_xml_int (a_node,(xmlChar*)key,0);
    sprintf(key,"p%d_y",i+1);
    pos[1][i] = sms_get_xml_int (a_node,(xmlChar*)key,0);
  }

  return;

err:
  if(pos)
    free(pos);

}


static void  setting_layout_create_icon(xmlNode *a_node,Layout *layout ,int id)
{
  SetLayout *setlayout=layout->private;
  SMS *sms=layout->sms;
  //int id;
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  //Icon *icon=(Icon *)g_malloc0(sizeof(Icon)+1);
  printf("id=%d\n" ,id);
  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
    goto err;
  setlayout->icons[id].name=g_strdup(val);
  setlayout->icons[id].size=sms_get_xml_int(a_node,(xmlChar*)"size",64);
  setlayout->icons[id].asize=sms_get_xml_int(a_node,(xmlChar*)"asize",setlayout->icons[id].size);     
  setlayout->icons[id].x=sms_get_xml_int(a_node,(xmlChar*)"x",setlayout->icons[id].x);     
  setlayout->icons[id].y=sms_get_xml_int(a_node,(xmlChar*)"y",setlayout->icons[id].y);   
  if(id == 0){
    setlayout->icons[0].cmd=g_strdup(xmlGetProp(a_node ,(xmlChar*)"exec"));
    printf("cmd=%s\n" ,setlayout->icons[0].cmd);
  }
  if(id == 1){
    setlayout->icons[1].cmd=g_strdup(xmlGetProp(a_node ,(xmlChar*)"exec"));
    printf("cmd=%s\n" ,setlayout->icons[1].cmd);
  }

  //icon
  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
    goto err;
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,setlayout->icons[id].size,setlayout->icons[id].size);
  if(!pixbuf)
    goto err; 
  setlayout->icons[id].icon=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);	
  g_object_set(setlayout->icons[id].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  val = xmlGetProp (a_node,(xmlChar*)"apic");
  if(!val)
    goto err;
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,setlayout->icons[id].asize,setlayout->icons[id].asize);
  if(!pixbuf)
    goto err;
  setlayout->icons[id].icon_a=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);
  g_object_set(setlayout->icons[id].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);

  //return test_layout->icons[0];


err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  //if(setlayout->icons[0])
  //main_layout_destroy_icon(setlayout->icons[0]);
  //return NULL;


}

static gboolean setting_layout_load_icons(xmlNode *a_node,Layout *layout)
{
  GdkPixbuf*  pixbuf;
  char pic[256];
  /*
     sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"theme,layout->name,LAN_NET_WORK_BG);
     pixbuf=sms_load_pic(pic,300,300); 
     icons->icon=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);	
     g_object_set(icons->icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
     */       

  SetLayout *setlayout=layout->private;
  xmlNode *cur_node = NULL;
  int i=0;
  Icon * icon;
  if(!a_node)
    return FALSE;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      if(strcmp(cur_node->name,"icon")==0){
        setting_layout_create_icon(cur_node,layout ,i);
        i++;
      }
    }
  }
  return TRUE ;

}

static gboolean setting_layout_load_title(xmlNode *a_node,Layout *layout)
{
  SetLayout *setlayout=layout->private;
  GooCanvasItem *text3;
  GooCanvasItem *text4;
  xmlNode *cur_node = a_node;
  xmlChar * val;
  char font[64];        
  char pic[256];

  if(!a_node)
    goto err;

  GooCanvasItem *logo;
  sprintf(pic ,"%s/%s/layout/%s/%s" ,SMS_RESOURCE_DIR ,"default" ,layout->name ,"logo.png");
  GdkPixbuf  *pixbuf_logo = gdk_pixbuf_new_from_file_at_size(pic ,120 ,138 ,NULL);
  logo = goo_canvas_image_new(layout->cavas ,pixbuf_logo ,0 ,0 ,NULL);
  goo_canvas_item_set_simple_transform(logo ,656 ,19 ,1 ,0);

  setlayout->title.x1=sms_get_xml_int(cur_node,(xmlChar*)"x1",0);
  setlayout->title.y1=sms_get_xml_int(cur_node,(xmlChar*)"y1",0);
  setlayout->title.x2=sms_get_xml_int(cur_node,(xmlChar*)"x2",0);
  setlayout->title.y2=sms_get_xml_int(cur_node,(xmlChar*)"y2",0);
  setlayout->title.size=sms_get_xml_int(cur_node,(xmlChar*)"size",10);

  val = xmlGetProp (cur_node,(xmlChar*)"color");
  if(val){
    setlayout->title.color=g_strdup(val);
  }
  val = xmlGetProp (cur_node,(xmlChar*)"font");
  if(val){
    setlayout->title.font=g_strdup(val);
  }

  sprintf(font ,"%s %d",setlayout->title.font ,setlayout->title.size);
  setlayout->title.text1=goo_canvas_text_new(layout->cavas,"",
      setlayout->title.x1,setlayout->title.y1,-1,GTK_ANCHOR_CENTER,
      "font", font,NULL);

  setlayout->title.text2=goo_canvas_text_new(layout->cavas,"",
      setlayout->title.x2,setlayout->title.y2,-1,GTK_ANCHOR_CENTER,
      "font", font,NULL);

  if(setlayout->title.color){
    g_object_set(setlayout->title.text1,"fill_color",setlayout->title.color,NULL);
    g_object_set(setlayout->title.text2,"fill_color",setlayout->title.color,NULL);
  }

  text3 = goo_canvas_text_new(layout->cavas,"网 络\n设 置",
      180,83,-1,GTK_ANCHOR_CENTER,
      "font", "Sans Bold black",NULL);

  text4 = goo_canvas_text_new(layout->cavas,"东信北邮家庭教育演示系统",
      600 ,444 ,-1 ,GTK_ANCHOR_CENTER ,
      "font"  ,"Sans red 20" ,NULL);
  g_object_set(text4,"fill_color","red",NULL);


  return TRUE;
err:
  return FALSE;

}

/*
 *hjq add for stop animate
 */

/*void setting_animation_finished (GooCanvasItem *item, gboolean stopped, gpointer user_data)
  {
  printf("~~~%s\n" ,__FUNCTION__);
  SetLayout *layout = (SetLayout*) user_data;
  if (stopped)
  {
  g_print("manually stop.\n");
  }
  else
  {
  g_print("auto stop.\n");
//goo_canvas_item_set_simple_transform(item , -100 ,90 ,1 ,0);
//goo_canvas_item_animate(item, 0, 90, 1, 0, TRUE, 4000, 100, GOO_CANVAS_ANIMATE_RESET);
setting_animate_control (layout, ANIMATE_RESTART);
}
}

gboolean setting_timeout_callback(gpointer user_data)
{
printf("~~~%s\n" ,__FUNCTION__);
SetLayout *layout = (SetLayout*) user_data;
setting_animate_control (layout, ANIMATE_CONTINUE);
return FALSE;
}*/

/*
 * 1: stop
 * 2: restart
 * 3: continue
 * */

void setting_animate_control (SetLayout *layout, int control)
{
  printf("~~~%s\n" ,__FUNCTION__);
  int i;
  Animate *animate;
  for (i=0; i<layout->animate_count; i++)
  {
    animate = layout->animates;
    switch (control)
    {
      case ANIMATE_STOP:
        animate_stop(animate);
        break;
      case ANIMATE_RESTART:
        animate_restart(animate);
        break;
      case ANIMATE_CONTINUE:
        animate_continue (animate);
        break;
      default:
        break;
    }
    animate = animate->next;
  }
}

#define ANIMATE_FREEZE   1
#define ANIMATE_RESET    2
#define ANIMATE_RESTART  3
#define ANIMATE_BOUNCE   4

static Animate * setting_layout_create_animate(xmlNode *a_node,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);

  SetLayout *setlayout=layout->private;
  Animate *animate;
  xmlChar * val;
  char pic[256];

  gdouble start_x;
  gdouble start_y;
  gdouble stop_x;
  gdouble stop_y;

  gdouble init_scale;
  gdouble init_rotation;
  gdouble animate_scale;
  gdouble animate_rotation;
  gboolean isAbsolute;
  gint total_time;
  gint step_time;
  GooCanvasAnimateType type;

  start_x = sms_get_xml_double (a_node, (xmlChar*)"start_x", 0);
  start_y = sms_get_xml_double (a_node, (xmlChar*)"start_y", 0);
  stop_x = sms_get_xml_double (a_node, (xmlChar*)"stop_x", 0);
  stop_y = sms_get_xml_double (a_node, (xmlChar*)"stop_y", 0);
  init_scale = sms_get_xml_double (a_node, (xmlChar*)"init_scale", 0);
  init_rotation = sms_get_xml_double (a_node, (xmlChar*)"init_rotation", 0);
  animate_scale = sms_get_xml_double (a_node, (xmlChar*)"animate_scale", 0);
  animate_rotation = sms_get_xml_double (a_node, (xmlChar*)"animate_rotation", 0);
  isAbsolute = sms_get_xml_boolean (a_node, (xmlChar*)"isAbsolute");
  total_time = sms_get_xml_int (a_node, (xmlChar*)"total_time", 0);
  step_time = sms_get_xml_int (a_node, (xmlChar*)"step_time", 0);

  switch (sms_get_xml_int (a_node, (xmlChar*)"type", 1))
  {
    case ANIMATE_FREEZE:
      type = GOO_CANVAS_ANIMATE_FREEZE;
      break;
    case ANIMATE_RESET:
      type = GOO_CANVAS_ANIMATE_RESET;
      break;
    case ANIMATE_RESTART:
      type = GOO_CANVAS_ANIMATE_RESTART;
      break;
    case ANIMATE_BOUNCE:
      type = GOO_CANVAS_ANIMATE_BOUNCE;
      break;
    default: break;
  }
  g_print ("startx: %f, starty: %f, stopx: %f, stopy: %f,init_scale: %f, animate_scale: %f, animate_rotation: %f, total_time: %d, step_time %d\n",
      start_x, start_y, stop_x, stop_y, init_scale, animate_scale, total_time, step_time);

  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
    goto err;
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  g_print ("pic path: %s", pic);

  animate = animate_new (layout->cavas, pic, start_x, start_y, stop_x, stop_y, init_scale, init_rotation,
      animate_scale, animate_rotation, isAbsolute, total_time, step_time, type);
  //g_signal_connect (G_OBJECT (animate->cavas), "animation-finished", G_CALLBACK (setting_animation_finished), setlayout);
  return animate;
err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  return NULL;
}

static gboolean setting_layout_load_animates(xmlNode *a_node, Layout *layout)
{
  SetLayout *setlayout=layout->private;

  xmlNode *cur_node = NULL;
  Animate * animate;

  if(!a_node)
    return FALSE;
  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE) {
      if(strcmp(cur_node->name,"animate")==0)
      {
        animate = setting_layout_create_animate(cur_node, layout);
        if(animate)
        {
          setlayout->animate_count++;
          if(setlayout->animates)
          {
            setlayout->animates->next = animate;
          }
          else
          {
            setlayout->animates = animate;
          }
        }
      }
    }
  }
  return TRUE;
}

static gboolean setting_layout_travel_xml(xmlNode *a_node,Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  SetLayout *setlayout=layout->private;
  xmlNode *cur_node = NULL;
  xmlNode *bg_node = NULL;
  xmlNode *icon_node = NULL;
  xmlNode *title_node = NULL;
  xmlNode *animate_node = NULL;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      printf("node type: Element, name: %s\n", cur_node->name);
      if(strcmp(cur_node->name,"bg")==0)
        bg_node=cur_node;
      else if(strcmp(cur_node->name,"icons")==0)
        icon_node=cur_node;
      else if(strcmp(cur_node->name,"title")==0)
        title_node=cur_node;
      else if(strcmp(cur_node->name,"animates")==0)
        animate_node = cur_node;
    }
  }

  if(!bg_node||!icon_node||!title_node||!animate_node)
    return FALSE;

  setting_layout_load_bg(bg_node,layout);
  setting_layout_load_animates (animate_node->children, layout);
  setting_layout_load_icons(icon_node->children,layout);
  setting_layout_load_title(title_node,layout);

  if(setlayout->micon_count>=2)
    return TRUE;

  return FALSE;


}

gboolean setting_layout_load_config(Layout *layout)
{
  gboolean ret=FALSE;
  xmlDoc *doc = NULL;
  xmlNode *root_element = NULL;
  SetLayout *setlayout=layout->private;
  char conf[256];
  sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name);
  printf("layout->name=%s\n" ,layout->name);

  doc = xmlReadFile(conf, NULL, 0);
  if (doc == NULL) {
    fprintf(stderr,"error: could not parse file %s\n", conf);
    return ret;
  }
  root_element = xmlDocGetRootElement(doc);

  if(root_element)
  {
    xmlNode *cur_node = NULL;
    for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
      if (cur_node->type == XML_ELEMENT_NODE) {
        ret=TRUE;
        break;
      }
    }
    if(ret)
      ret=setting_layout_travel_xml(cur_node->children,layout);
  }

  xmlFreeDoc(doc);
  xmlCleanupParser();
  return ret;

}


void setting_layout_reset_icons(Layout *layout)
{

  SetLayout *setlayout=layout->private;
  Icon * icon=setlayout->icons;
  //test_layout->focus_id=0;

  g_object_set(setlayout->icons[0].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  g_object_set(setlayout->icons[0].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  goo_canvas_item_translate(setlayout->icons[0].icon,
      setlayout->icons[0].x,setlayout->icons[0].y);    
  goo_canvas_item_translate(setlayout->icons[0].icon_a,
      setlayout->icons[0].x,setlayout->icons[0].y);    
  g_object_set(setlayout->title.text1,"text",setlayout->icons[0].name,NULL);
  //g_object_set(test_layout->title.text1,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  //focus_id++;
  g_object_set(setlayout->icons[1].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  g_object_set(setlayout->icons[1].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  goo_canvas_item_translate(setlayout->icons[1].icon,
      setlayout->icons[1].x,setlayout->icons[1].y);    
  goo_canvas_item_translate(setlayout->icons[1].icon_a,
      setlayout->icons[1].x,setlayout->icons[1].y);    
  g_object_set(setlayout->title.text2,"text",setlayout->icons[1].name,NULL);
  g_object_set(setlayout->title.text2,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

}


static gboolean setting_layout_init(Layout *layout,gpointer data)
{
  printf("call %s\n",__FUNCTION__);

  SetLayout *setlayout=(SetLayout *)g_malloc0(sizeof(SetLayout)+1);
  layout->private=(void *)setlayout;

  layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

  if(!setting_layout_load_config(layout))
  {
    fprintf(stderr,"%s:load config err\n",__FUNCTION__);
    //return FALSE;//exit(-1);
  }
  setting_layout_reset_icons(layout);

  return TRUE;
}

static gboolean setting_layout_clear(Layout *layout,gpointer data)
{
  SetLayout *setlayout;
  int i;

  printf("call %s\n",__FUNCTION__);

  setlayout=layout->private;

  g_object_unref(setlayout->bg);	

  g_free(setlayout);
}

static void change_icon(Layout *layout, int num1 ,int num2)
{
  printf("~~~%s\n " ,__FUNCTION__);
  SetLayout *setlayout;
  setlayout=layout->private;
  // printf("num=%d\n" ,num);
  g_object_set(setlayout->icons[num1].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  g_object_set(setlayout->icons[num1].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  //g_object_set(test_layout->title.text1,"text",test_layout->icons[num1].name,NULL);

  g_object_set(setlayout->icons[num2].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  g_object_set(setlayout->icons[num2].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  if(num1==0){
    g_object_set(setlayout->title.text1,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    g_object_set(setlayout->title.text2,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  }
  else{
    g_object_set(setlayout->title.text1,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
    g_object_set(setlayout->title.text2,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  }
}

static void setting_layout_move_icon(Layout *layout,gboolean is_left)
{
  SetLayout *setlayout;
  setlayout=layout->private;
  int num1;
  int num2;
  SMS *sms = layout->sms ;
  if(is_left){
    num1=abs((focus_id)++)%2;
    num2=abs((focus_id))%2;
    printf("focus_id=%d ,%d\n" ,num1 ,num2);
    change_icon(layout ,num1 ,num2);
    //g_object_set(test_layout->title.text1,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    sms->busy = FALSE ;
  }
  else{
    num1=abs((focus_id)--)%2;
    num2=abs((focus_id))%2;
    printf("focus_id=%d ,%d\n" ,num1 ,num2);
    change_icon(layout ,num1 ,num2);
    sms->busy = FALSE ;
  }
}

  /***********stjq add*******************/
static gint save_to_network_config(gboolean is_lan)
{
 FILE *fp = NULL;
 remove(NETWORK_CONFIG);
 if ((fp = fopen(NETWORK_CONFIG, "w+")) == NULL)
 {
	printf("cannot open file: %s!\n", NETWORK_CONFIG);
        return -1;
 }
 if(is_lan)
 {
	fputs("eth0", fp);
 }
 else
 {
	fputs("NONE", fp);
 }
    fclose(fp);
    return 0;
}


static gboolean setting_layout_key_press(Layout * layout,GdkEventKey *event)
{
  SetLayout *setlayout;
  int i;
  Icon *icon;
  //char *command;

  setlayout=layout->private;

  if(event->keyval==GDK_Right){
    //setting_animate_control (setlayout, ANIMATE_STOP);
    //g_timeout_add(300, (GSourceFunc) setting_timeout_callback, setlayout);
    setting_layout_move_icon(layout ,TRUE);
    return TRUE;
  }else if(event->keyval==GDK_Left){
    //setting_animate_control (setlayout, ANIMATE_STOP);
    //g_timeout_add(300, (GSourceFunc) setting_timeout_callback, setlayout);
    setting_layout_move_icon(layout ,FALSE);
    return TRUE;
  }else if(event->keyval==GDK_Return){
    if(0==(abs((focus_id))%2)){
      printf("lan!\n");
      //system(setlayout->icons[0].cmd);
      //system("echo 0 > /sys/power/iwifi");
      save_to_network_config(TRUE);
      printf("cmd=%s\n" ,setlayout->icons[0].cmd);
      sms_change_win_to(layout->sms,LAYOUT_WIN_LAN_SETTING);
    }
    else{
      printf("wireless!\n");
      //system(setlayout->icons[1].cmd);
      //system("echo 1 > /sys/power/iwifi");
      save_to_network_config(FALSE);
      printf("cmd=%s\n" ,setlayout->icons[1].cmd);
      sms_change_win_to(layout->sms,LAYOUT_WIN_WIFI_SETTING);
      //sms_change_win_to(layout->sms,LAYOUT_WIN_WIFI_LISTS);
    }
    return TRUE;
  }else if(event->keyval==GDK_Escape){		
    sms_change_win_to(layout->sms,LAYOUT_WIN_MAIN);
    //setting_animate_control (layout, ANIMATE_STOP);
    return TRUE;
  }else if(event->keyval==GDK_1){
    printf("~~~~~update1!\n");
    system("update1.sh");
    printf("download update1.sh ok~\n");
  }else if(event->keyval==GDK_2){
    printf("~~~~~update2!\n");
    system("update2.sh");
    printf("download update2.sh ok~\n");
  }
  printf("%s : propagate the event\n",__FUNCTION__);
  return FALSE;
}


Layout *setting_layout_new(GooCanvasItem * parent,gpointer sms)
{
  Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN,LAYOUT_WIN_SETTING,
      SETTING_LAYOUT_NAME,sms);

  layout_set_func(layout,"init",setting_layout_init);
  layout_set_func(layout,"clear",setting_layout_clear);
  layout_set_func(layout,"key-press",setting_layout_key_press);
  return layout;
}
